package cn.meibang.common.beauty.provider.dao;

import cn.apestar.core.database.BaseMapper;
import cn.meibang.common.beauty.provider.domain.BeautyConsultationOrderUser;
import cn.meibang.common.beauty.provider.request.ListOrderUserRequest;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface BeautyConsultationOrderUserMapper extends BaseMapper {

    @Select({
            "select count(1) from beauty_consultation_order_user ",
            "where create_date >= #{dateStart} and create_date <= #{dateEnd} ",
            "and consultant_id = #{consultantId} "
    })
    @ResultType(Integer.class)
    int countTotal(@Param("dateStart") Date dateStart,
                   @Param("dateEnd") Date dateEnd,
                   @Param("consultantId") Long consultantId);

    @Select({
            "select count(1) from beauty_consultation_order_user ",
            "where deal_date >= #{dateStart} and deal_date <= #{dateEnd} ",
            "and consultant_id = #{consultantId} and is_deal = '1'"
    })
    @ResultType(Integer.class)
    int countDeal(@Param("dateStart") Date dateStart,
                   @Param("dateEnd") Date dateEnd,
                   @Param("consultantId") Long consultantId);

    @Delete({"delete from beauty_consultation_order_user", "where id = #{id,jdbcType=BIGINT}"})
    int deleteByPrimaryKey(Long id);

    @Insert({"insert into beauty_consultation_order_user (id, tag, user_id, openid, ",
            "store_id, consultant_id, title, ", "order_id, order_type, create_by, ",
            "create_date, is_deal, deal_date)", "values (#{id,jdbcType=BIGINT}, #{tag,jdbcType=VARCHAR}, #{userId}, #{openid}, ",
            "#{storeId}, #{consultantId,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, ",
            "#{orderId,jdbcType=BIGINT}, #{orderType}, #{createBy,jdbcType=BIGINT}, ",
            "#{createDate,jdbcType=TIMESTAMP},#{isDeal},#{dealDate})"})
    @SelectKey(before = false, keyProperty = "id", resultType = Long.class, statement = "SELECT LAST_INSERT_ID() AS id")
    int insert(BeautyConsultationOrderUser record);

    @InsertProvider(type = BeautyConsultationOrderUserSqlProvider.class, method = "insertSelective")
    int insertSelective(BeautyConsultationOrderUser record);

    @Select({"select", "id, tag, store_id, user_id, openid, consultant_id, title, order_id, order_type, create_by, create_date,",
            "is_deal, deal_date",
            "from beauty_consultation_order_user",
            "where id = #{id,jdbcType=BIGINT}"})
    @Results({@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR),
            @Result(column = "store_id", property = "storeId", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "openid", property = "openid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consultant_id", property = "consultantId", jdbcType = JdbcType.BIGINT),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "order_id", property = "orderId", jdbcType = JdbcType.BIGINT),
            @Result(column = "order_type", property = "orderType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_deal", property = "isDeal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "deal_date", property = "dealDate", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP)})
    BeautyConsultationOrderUser selectByPrimaryKey(Long id);

    @SelectProvider(type = BeautyConsultationOrderUserSqlProvider.class, method = "selectByListRequest")
    @Results({@Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR),
            @Result(column = "store_id", property = "storeId", jdbcType = JdbcType.BIGINT),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.BIGINT),
            @Result(column = "openid", property = "openid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "consultant_id", property = "consultantId", jdbcType = JdbcType.BIGINT),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "order_id", property = "orderId", jdbcType = JdbcType.BIGINT),
            @Result(column = "order_type", property = "orderType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_deal", property = "isDeal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "deal_date", property = "dealDate", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "create_by", property = "createBy", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_date", property = "createDate", jdbcType = JdbcType.TIMESTAMP)})
    List<BeautyConsultationOrderUser> selectByListRequest(ListOrderUserRequest request);

    @UpdateProvider(type = BeautyConsultationOrderUserSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(BeautyConsultationOrderUser record);

    @Update({"update beauty_consultation_order_user", "set tag = #{tag,jdbcType=VARCHAR},",
            "store_id = #{storeId},",
            "user_id = #{userId},",
            "openid = #{openid},",
            "consultant_id = #{consultantId,jdbcType=BIGINT},",
            "title = #{title,jdbcType=VARCHAR},",
            "order_id = #{orderId,jdbcType=BIGINT},",
            "order_type = #{orderType,jdbcType=VARCHAR},",
            "create_by = #{createBy,jdbcType=BIGINT},",
            "is_deal = #{isDeal},",
            "deal_date = #{dealDate},",
            "create_date = #{createDate,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"})
    int updateByPrimaryKey(BeautyConsultationOrderUser record);
}